package com.example.edianzu.somur.login.mvp.model;

import com.example.edianzu.somur.api.bean.LoginInfo;
import com.example.edianzu.somur.api.constans.AppContents;
import com.example.edianzu.somur.api.manager.APIManager;
import com.example.edianzu.somur.login.mvp.OnLoginListener;
import com.example.edianzu.somur.utils.content.ValidateUtils;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Created by edianzu on 2017/7/10.
 */

public class LoginModelImpl implements ILoginModel{

    @Override
    public void login(String phoneNumber, String passWord, final OnLoginListener onLoginListener) {

        if (passWord != null&& phoneNumber !=null) {
            if (phoneNumber.length()==11&& ValidateUtils.isMobileNO(phoneNumber)) {

                APIManager.getApiManagerInstance().getLoginInfo(new Observer<LoginInfo>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull LoginInfo loginInfo) {

                        /*如果账号信息有误，进行提示*/
                        if (loginInfo!=null&&loginInfo.getStatus()== AppContents.STATUS) {

                            onLoginListener.onLoginSuccess(loginInfo);
                        }else {

                            onLoginListener.onLoginError();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                        onLoginListener.onLoginError();
                    }

                    @Override
                    public void onComplete() {

                    }
                },phoneNumber,passWord,"7c5be769f7854d9b97205794b276673d");

            }else {

                onLoginListener.onLoginError();
            }
        }
    }
}
